package com.woniu.gf.pawn.shopweb.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.gf.pawn.comm.core.domain.Management;
import com.woniu.gf.pawn.comm.core.domain.Shop;
import com.woniu.gf.pawn.comm.core.domain.Store;
import com.woniu.gf.pawn.comm.core.dto.PageReultDto;
import com.woniu.gf.pawn.comm.core.dto.ResultDto;
import com.woniu.gf.pawn.comm.core.exception.PawnExceptionCode;
import com.woniu.gf.pawn.management.client.ManagementClient;
import com.woniu.gf.pawn.management.param.SelectManagementByRoleIdParam;
import com.woniu.gf.pawn.pagedto.PageDto;
import com.woniu.gf.pawn.pageparam.PageParam;
import com.woniu.gf.pawn.shopdto.ShopDto;
import com.woniu.gf.pawn.shopparam.ShopParam;
import com.woniu.gf.pawn.shopweb.service.IManagementService;
import com.woniu.gf.pawn.shopweb.service.IShopService;
import com.woniu.gf.pawn.shopweb.service.IShopStoreService;
import com.woniu.gf.pawn.store.client.StoreClient;
import com.woniu.gf.pawn.store.param.SelectStoreByShopIdParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TL
 * @since 2020-10-13
 */
@CrossOrigin()
@RestController
@RequestMapping("/shop")
@Slf4j
@Api(tags = "门店")
public class ShopController {

    @Autowired
    private IShopService shopService;
    @Autowired
    private IShopStoreService shopStoreService;
    @Autowired
    private IManagementService managementService;
    @Autowired
    private ManagementClient managemerClient;
    @Autowired
    private StoreClient storeClient;

    /**
     * 新增门店
     * @param shopDto
     * @return
     */

    @PostMapping("addShop")
    @ApiOperation("新增门店")
    public ResultDto addShop(@RequestBody ShopDto shopDto){
        log.info("参数：{}",shopDto);
        String shopName = shopDto.getShopName();
        ShopParam shopParam = new ShopParam();
        BeanUtils.copyProperties(shopDto,shopParam);
        int result= shopService.addShop(shopParam);
       return ResultDto.success(200,"添加成功");
    }

    /**
     * 查询门店信息（按状态查询（使用，停用）、模糊查询（门店名称，联系人名称、联系人手机号））
     * @param pageDto
     * @return
     */

    @GetMapping("findShopInfo")
    @ApiOperation("查询门店信息")
    public PageReultDto findShopInfo(PageDto pageDto){
        log.info("参数：{}",pageDto);
        if (pageDto.getPageIdx() == null) {
            pageDto.setPageIdx(1);
        }
        if (pageDto.getPageSize() == null) {
            pageDto.setPageSize(10);
        }
        PageParam pageParam = new PageParam();
        if (pageDto.getQuery() != null) {

            Management management=managementService.findManage(pageDto.getQuery());
            if (management != null) {
                pageParam.setManagerId(management.getManagementId());
            }else{
                pageParam.setShopName(pageDto.getQuery());
            }
        }
        BeanUtils.copyProperties(pageDto,pageParam);
        IPage<Shop> shops=shopService.findShopInfo(pageParam);
        List<ShopDto> shopDtos = new ArrayList<>();
        if (shops != null) {
            for (Shop shop : shops.getRecords()) {
                List<Store> stores=shopService.findStore(shop.getShopId());
                Management management= managementService.findManagerById(shop.getManagerId());
                ShopDto shopDto = new ShopDto();
                BeanUtils.copyProperties(shop,shopDto);
                shopDto.setStores(stores);
                shopDto.setManagementName(management.getManagementName());
                shopDto.setManagementPhone(management.getManagementPhone());
                shopDtos.add(shopDto);
            }
        }


        return PageReultDto.success(shops.getTotal(),pageDto.getPageIdx(),pageDto.getPageSize(),shopDtos);
    }

    /**
     * 按门店ID查询门店信息
     * @param shopId
     * @return
     */

    @GetMapping("findOneShopInfo/{shopId}")
    @ApiOperation("按门店ID查询门店信息")
    public ResultDto findOneShopInfo(@PathVariable("shopId") Integer shopId){
        log.info("shopId:{}",shopId);
        ShopParam shopParam=shopService.findOneShopInfo(shopId);
        if (shopParam == null) {
            return ResultDto.fail(500,"没有该门店或该门店已停用");
        }
        List<Store> stores = shopService.findStore(shopId);
        shopParam.setStores(stores);
        ShopDto shopDto = new ShopDto();
        BeanUtils.copyProperties(shopParam,shopDto);
        return ResultDto.success(200,"查询成功",shopDto);

    }

    /**
     * 修改门店基本信息和仓库
     * @param shopDto
     * @return
     */

    @PostMapping("updateShop")
    @Transactional
    @ApiOperation("修改门店基本信息和仓库")
    public ResultDto updateShop(@RequestBody ShopDto shopDto){
        log.info("参数-shopDto:{}",shopDto);
        ShopParam shopParam = new ShopParam();
        BeanUtils.copyProperties(shopDto,shopParam);
        shopService.updateShop(shopParam);
        return ResultDto.success(200,"修改成功");
    }

    /**
     * 删除门店
     * @param shopId
     * @return
     */

    @GetMapping("deleteShop")
    @Transactional
    @ApiOperation("删除门店")
    public ResultDto deleteShop(@RequestParam("shopId") Integer shopId){
        log.info("参数-shopId:{}",shopId);
        int result =shopService.deleteShop(shopId);
        return ResultDto.success(200,"删除成功");
    }

    @ApiOperation("查询拥有门店管理员角色的管理员集合")

    @GetMapping("/selectManagementByRoleId")
    public ResultDto selectManagementByRoleId(){
        SelectManagementByRoleIdParam roleIdParam = new SelectManagementByRoleIdParam();
        roleIdParam.setRoleId(2);
        ResultDto resultDto = managemerClient.selectManagementByRoleId(roleIdParam);
        return resultDto;
    }


    @GetMapping("selectStoreByShopId")
    public ResultDto SelectStoreByShopId(@RequestParam("shopId") Integer shopId ){
        SelectStoreByShopIdParam selectStoreByShopIdParam =new SelectStoreByShopIdParam();
        selectStoreByShopIdParam.setShopId(shopId);
        return storeClient.SelectStoreByShopId(selectStoreByShopIdParam);
    }

    @GetMapping("selectStoreNotByShopId")
    public ResultDto selectStoreNotByShopId(@RequestParam("shopId") Integer shopId ){
        SelectStoreByShopIdParam selectStoreByShopIdParam =new SelectStoreByShopIdParam();
        selectStoreByShopIdParam.setShopId(shopId);
        return storeClient.selectStoreNotByShopId(selectStoreByShopIdParam);
    }
    /**
     *@Description: 查询所有已使用仓库
     *@return:
     */

    @GetMapping("/selectAllStores")
    @ApiOperation("查询所有已使用仓库")
    public ResultDto selectAllStores(){
        ResultDto resultDto = storeClient.selectAllStores();

        return resultDto;
    }



}

